import 'package:huxiaodui/api/api.dart';
import 'package:huxiaodui/api/model/unit_model.dart';
import 'package:huxiaodui/utils/global_controller.dart';
import 'package:huxiaodui/view/index/map_page/map_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
class UnitsComponent extends StatefulWidget {

  final List<UnitModel> units;
  final Function? onBottom;

  const UnitsComponent({super.key, required this.units, this.onBottom});

  @override
  State<StatefulWidget> createState() => _UnitsComponentState();

}
class _UnitsComponentState extends State<UnitsComponent> {

  @override
  Widget build(BuildContext context) {
    ScrollController controller = ScrollController();
    controller.addListener(() {
      if (controller.position.pixels ==
          controller.position.maxScrollExtent) {
        if(widget.onBottom!=null)widget.onBottom!();
      }
    });

    getLine(String text) {
      return Container(
        padding: const EdgeInsets.only(top: 1,bottom: 1),
        width: 200,
        child: Text(
          text,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
      );
    }

    return ListView.builder(
      controller: controller,
      itemCount: widget.units.length,
      itemBuilder: (BuildContext context, int index) {
        return InkWell(
          onTap: () {},
          child: Container(
            padding: const EdgeInsets.all(20),
            decoration: const BoxDecoration(
              border: Border(
                bottom: BorderSide(width: 0.3),
              ),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Column(
                  children: [
                    getLine("编号：${widget.units[index].code}"),
                    getLine("名称：${widget.units[index].name}"),
                    getLine("管理员：${widget.units[index].manager}"),
                    getLine("联系电话：${widget.units[index].tel}"),
                    getLine("地址：${widget.units[index].addr}"),
                    getLine("派出所：${widget.units[index].policeStation}"),
                  ],
                ),
                Column(
                  children: [
                    !widget.units[index].isJoin
                        ? ElevatedButton(
                            onPressed: () {
                              Api.request(
                                url: UnitUrl.join,
                                data: {"id": widget.units[index].id},
                                success: (res) {
                                  setState(() {
                                    widget.units[index].isJoin = true;
                                  });
                                },
                                isToast: true,
                              );
                            },
                            child: const Row(
                              children: [
                                Icon(Icons.add),
                                Text("加入"),
                              ],
                            ),
                          )
                        : ElevatedButton(
                            onPressed: () {
                              Api.request(
                                url: UnitUrl.quit,
                                data: {"id": widget.units[index].id},
                                success: (res) {
                                  setState(() {
                                    widget.units[index].isJoin = false;
                                  });
                                },
                                isToast: true,
                              );
                            },
                            style: ButtonStyle(
                              backgroundColor:
                                  MaterialStateProperty.all(Colors.red),
                            ),
                            child: const Row(
                              children: [
                                Icon(Icons.remove),
                                Text("退出"),
                              ],
                            ),
                          ),
                    ElevatedButton(
                      onPressed: () {
                        if(widget.units[index].latLng!=null) {
                          GlobalController.toFirst(showDrawer: false);
                          MapController().moveMap(latLng: widget.units[index].latLng!);
                        }else{
                          EasyLoading.showInfo("没有该单位的位置");
                        }
                      },
                      style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all(Colors.cyan),
                      ),
                      child: const Row(
                        children: [
                          Icon(Icons.add_circle_outline),
                          Text("位置"),
                        ],
                      ),
                    )
                  ],
                ),


              ],
            ),
          ),
        );
      },
    );
  }
}
